using System.ComponentModel;
using System.Text.Json.Serialization;

namespace JGSY.CMS.LowCode.Platform.Application.DTOs.Common;

/// <summary>
/// 统一API响应格式
/// </summary>
/// <typeparam name="T">响应数据类型</typeparam>
public class ApiResponse<T>
{
    /// <summary>
    /// 是否成功
    /// </summary>
    /// <example>true</example>
    [JsonPropertyName("success")]
    public bool Success { get; set; }

    /// <summary>
    /// 响应消息
    /// </summary>
    /// <example>操作成功</example>
    [JsonPropertyName("message")]
    public string Message { get; set; } = string.Empty;

    /// <summary>
    /// 错误码(失败时提供)
    /// </summary>
    /// <example>null</example>
    [JsonPropertyName("errorCode")]
    public string? ErrorCode { get; set; }

    /// <summary>
    /// 响应数据
    /// </summary>
    [JsonPropertyName("data")]
    public T? Data { get; set; }

    /// <summary>
    /// 时间戳
    /// </summary>
    /// <example>2025-08-08T10:30:00.000Z</example>
    [JsonPropertyName("timestamp")]
    public DateTime Timestamp { get; set; } = DateTime.UtcNow;

    /// <summary>
    /// 请求跟踪ID
    /// </summary>
    /// <example>req_1234567890abcdef</example>
    [JsonPropertyName("traceId")]
    public string? TraceId { get; set; }

    /// <summary>
    /// 分页信息(列表查询时提供)
    /// </summary>
    [JsonPropertyName("pagination")]
    public PaginationInfo? Pagination { get; set; }

    /// <summary>
    /// 创建成功响应
    /// </summary>
    /// <param name="data">响应数据</param>
    /// <param name="message">成功消息</param>
    /// <param name="traceId">跟踪ID</param>
    /// <returns>成功响应对象</returns>
    public static ApiResponse<T> Ok(T data, string message = "操作成功", string? traceId = null)
    {
        return new ApiResponse<T>
        {
            Success = true,
            Message = message,
            Data = data,
            TraceId = traceId,
            Timestamp = DateTime.UtcNow
        };
    }

    /// <summary>
    /// 创建失败响应
    /// </summary>
    /// <param name="message">错误消息</param>
    /// <param name="errorCode">错误码</param>
    /// <param name="traceId">跟踪ID</param>
    /// <returns>失败响应对象</returns>
    public static ApiResponse<T> Fail(string message, string? errorCode = null, string? traceId = null)
    {
        return new ApiResponse<T>
        {
            Success = false,
            Message = message,
            ErrorCode = errorCode,
            TraceId = traceId,
            Timestamp = DateTime.UtcNow
        };
    }

    /// <summary>
    /// 创建分页响应
    /// </summary>
    /// <param name="data">数据列表</param>
    /// <param name="currentPage">当前页码</param>
    /// <param name="pageSize">每页大小</param>
    /// <param name="totalCount">总记录数</param>
    /// <param name="message">响应消息</param>
    /// <param name="traceId">跟踪ID</param>
    /// <returns>分页响应对象</returns>
    public static ApiResponse<T> PagedOk(T data, int currentPage, int pageSize, int totalCount, 
        string message = "查询成功", string? traceId = null)
    {
        var totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
        return new ApiResponse<T>
        {
            Success = true,
            Message = message,
            Data = data,
            TraceId = traceId,
            Timestamp = DateTime.UtcNow,
            Pagination = new PaginationInfo
            {
                CurrentPage = currentPage,
                PageSize = pageSize,
                TotalCount = totalCount,
                TotalPages = totalPages,
                HasNext = currentPage < totalPages,
                HasPrevious = currentPage > 1
            }
        };
    }
}

/// <summary>
/// 分页信息
/// </summary>
public class PaginationInfo
{
    /// <summary>
    /// 当前页码
    /// </summary>
    /// <example>1</example>
    [JsonPropertyName("currentPage")]
    public int CurrentPage { get; set; }

    /// <summary>
    /// 每页大小
    /// </summary>
    /// <example>20</example>
    [JsonPropertyName("pageSize")]
    public int PageSize { get; set; }

    /// <summary>
    /// 总记录数
    /// </summary>
    /// <example>150</example>
    [JsonPropertyName("totalCount")]
    public int TotalCount { get; set; }

    /// <summary>
    /// 总页数
    /// </summary>
    /// <example>8</example>
    [JsonPropertyName("totalPages")]
    public int TotalPages { get; set; }

    /// <summary>
    /// 是否有下一页
    /// </summary>
    /// <example>true</example>
    [JsonPropertyName("hasNext")]
    public bool HasNext { get; set; }

    /// <summary>
    /// 是否有上一页
    /// </summary>
    /// <example>false</example>
    [JsonPropertyName("hasPrevious")]
    public bool HasPrevious { get; set; }
}

/// <summary>
/// 标准化的空响应
/// </summary>
public class EmptyResponse
{
    /// <summary>
    /// 空响应实例
    /// </summary>
    public static readonly EmptyResponse Instance = new();
}
